<template>
	<view class="store">
		<gyd-navbar back-color="#fff" :title="title"></gyd-navbar>

		<view class="search">
			<uni-search-bar placeholder="请输入关键字" cancelButton="none" @input="handleSearch"
				@clear="clearSearch"></uni-search-bar>
		</view>

		<view class="content-list">
			<view class="content-item" v-for="(item,index) in list" :key="index">
				<view class="title">
					<view class="left">店铺类型：<text>{{item.store_type_str}}</text></view>
					<view class="right">店铺状态：<text>{{item.status_str}}</text></view>
				</view>
				<view class="content">
					<image :src="item.store_logo"></image>
					<view class="info">
						<view>店铺名称：{{item.store_name || '待完善'}}</view>
						<view class="contact">
							<text class="name">联系人：{{item.contact_name || '待完善'}}</text>
							<text class="mobile">联系电话：{{item.contact_phone || '待完善'}}</text>
						</view>
						<view>店铺地址：{{item.street_address || '待完善'}}</view>
					</view>
				</view>
			</view>

			<view class="empty-data" v-if="list.length===0">暂未有店铺入驻</view>
		</view>
	</view>
</template>

<script setup>
	import request from '@/utils/request';
	import cache from '@/utils/cache';
	import util from '@/utils/util';
	import {
		ref,
		reactive,
		toRefs,
		inject,
	} from 'vue';
	import {
		onLoad,
		onShow,
		onReady,
		onPullDownRefresh,
		onReachBottom,
	} from '@dcloudio/uni-app';

	const state = reactive({
		title: '店铺管理',
		themeColor: cache.get('themeColor'),
		imgHost: util.requestUrl(),
		isMore: true,
		list: [],
		params: {
			merchant_id: 0,
			keyword: '',
			skip: 0,
		}
	});

	//在页面所有元素加载完成后执行（只执行一次）
	onLoad((opt) => {
		console.log('onLoad');
		if (opt.hasOwnProperty('id')) {
			console.log('传递参数', opt.id);
			state.params.merchant_id = opt.id;
		}
		getStoreList();
	});

	//在页面由不可见变为可见时执行（只要页面显示都会执行）
	onShow(() => {
		console.log('onShow');
	})

	//在页面解析完成之后执行
	onReady(() => {
		console.log('onReady');
	})

	onPullDownRefresh(() => {
		console.log('下拉刷新');
		setTimeout(() => {
			uni.stopPullDownRefresh();
		}, 1000);
		state.isMore = true;
		state.list = [];
		getStoreList();
	})

	onReachBottom(() => {
		console.log('触底加载更多');
		getStoreList();
	})

	function getStoreList() {
		if (!state.isMore) {
			util.showTip('没有更多了');
			return;
		}
		state.params.skip = state.list.length;
		console.log('查询条件', state.params);
		request.get('agent.merchant/storeList', state.params).then(res => {
			console.log('店铺列表', res);
			state.isMore = res.data.is_more;
			if (state.list.length == 0) {
				state.list = res.data.data;
			} else {
				state.list = state.list.concat(res.data.data);
			}
		});
	}

	function handleSearch(e) {
		console.log('搜索', e);
		state.params.keyword = e;
		state.isMore = true;
		state.list = [];
		getStoreList();
	}

	function clearSearch(e) {
		console.log('清除', e);
		state.params.keyword = '';
	}

	function pageRoute(url) {
		uni.navigateTo({
			url
		});
	}

	function pageTabRoute(url) {
		uni.reLaunch({
			url
		});
	}

	const {
		title,
		themeColor,
		imgHost,
		isMore,
		list,
		params,
	} = toRefs(state);
</script>

<style lang="scss">
	.store {
		.search {
			background: #fff;
			padding: 10rpx;
		}

		.content-list {
			padding-top: 20rpx;
			/* 根据按钮高度调整，防止内容被按钮遮挡 */
			padding-bottom: 50px;
			position: relative;

			.content-item {
				background: #fff;
				border-bottom: 1rpx solid #dadbde;

				&:last-child {
					border-bottom: none;
				}

				.title {
					display: flex;
					justify-content: space-between;
					padding: 10rpx 20rpx;

					.left {
						// padding-left: 20rpx;
					}

					.right {
						// padding-right: 20rpx;
					}
				}

				.content {
					display: flex;
					padding: 10rpx;

					image {
						width: 120rpx;
						height: 120rpx;
					}

					.info {
						padding-left: 20rpx;

						.contact {
							.name {}

							.mobile {
								margin-left: 50rpx;
							}
						}
					}
				}
			}

			.empty-data {
				margin-bottom: $m24;
				padding: $m24;
				background-color: #fff;
				border-radius: $radius-16;
				text-align: center;
				align-items: center;
				justify-content: center;
			}
		}
	}
</style>